/**
 * 
 */
package com.Hib.Recipes;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.Hib.Domain.Customer;
import com.Hib.Domain.CustomerId;
import com.Hib.Util.SessionManager;

/**
 * @author Ignatiues charles arun
 * 
 *         Aug 29, 2010
 */
public class CustomerDAO
	{

		private static SessionFactory sessionFactory;

		/**
		 * @param args
		 */
		public static void main(String[] args)
			{
				// TODO Auto-generated method stub
				SessionFactory sessionFactory = getSession();
				Session session = sessionFactory.openSession();
				session.beginTransaction();

				Customer customer = new Customer();
				customer.setId(new CustomerId("ch", "126(5)432"));
				customer.setFirstName("Arun");
				customer.setLastName("Kumar");
				customer.setAddress("Honkong");
				customer.setEmail("Charlesarun@aol.in");
				session.saveOrUpdate(customer);
				session.flush();
				session.beginTransaction().commit();
				session.close();

				System.out.println("Object persisted into DB");

				// Read Persistent object from DB
				read();

			}

		private static SessionFactory getSession()
			{
				sessionFactory = SessionManager.getSessionfactory();
				return sessionFactory;

			}

		private static void read()
			{
				List<Customer> customer = new ArrayList<Customer>();
				sessionFactory = SessionManager.getSessionfactory();
				Session session = sessionFactory.openSession();
				CustomerId customerid = new CustomerId("cn", "123(5)432");
				// session.load(Customer.class, customerid);
				// customer = (List<Customer>) session.load(Customer.class,
				// customerid);
				Customer objCustomer = (Customer) session.get(Customer.class,
						customerid);
				// customer = (List<Customer>) session.get(Customer.class,
				// customerid);

				System.out.println(objCustomer.getFirstName().toString());
				System.out.println(objCustomer.getLastName().toString());
				System.out.println(objCustomer.getEmail());
				System.out.println(objCustomer.getId().getCountryCode()
						.toString());
				System.out
						.println(objCustomer.getId().getIdCardNo().toString());
				session.close();

				// session.beginTransaction().commit();

			}

		/**
		 * @return the sessionFactory
		 */
		public static SessionFactory getSessionFactory()
			{
				return sessionFactory;
			}

	}
